package com.lap.auth.domain.menu;

import com.lap.auth.domain.menu.valueobject.Meta;
import com.lap.auth.domain.shared.errors.UserError;
import com.lap.framework.ddd.AggregateRoot;
import com.lap.framework.exception.BizException;
import java.util.List;
import lombok.Getter;

@Getter
public class Menu extends AggregateRoot {

  private final Integer id;
  private final Integer pid;
  private final String pidPath;
  private final MenuType type;
  private final String path;
  private final String name;
  private final String component;
  private final String redirect;
  private final String permCode;
  private final String link;
  private final Integer sortOrder;
  private final Meta meta;

  public Menu(
      Integer id,
      Integer pid,
      String pidPath,
      MenuType type,
      String path,
      String name,
      String component,
      String redirect,
      String permCode,
      String link,
      Integer sortOrder,
      Meta meta) {
    this.id = id;
    this.pid = pid;
    this.pidPath = pidPath;
    this.type = type;
    this.path = path;
    this.name = name;
    this.component = component;
    this.redirect = redirect;
    this.permCode = permCode;
    this.link = link;
    this.sortOrder = sortOrder;
    this.meta = meta;
  }

  public boolean isNotContainer() {
    return !this.type.isContainer();
  }

  public void assertCanChangeMenuType(MenuType type, List<Menu> child) {
    if (type == this.getType()) return;
    if (isNotContainer() && !child.isEmpty()) {
      BizException.interrupt(UserError.MENU_CONTAINER);
    }
  }

  public void canCreateChild() {
    if (isNotContainer()) {
      BizException.interrupt(UserError.MENU_NOT_CONTAINER);
    }
  }

  public void canDelete(List<Menu> child) {
    if (!child.isEmpty()) {
      BizException.interrupt(UserError.MENU_HAS_CHILD);
    }
  }

  public boolean isNew() {
    return this.id == null;
  }
}
